package edu.pku.sei.act.taskStruct;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.dom4j.Element;

public class Task extends Ident{

	public State initState;
	public Transition initTrans;
	public Map<String, Role> roles;
	
	public void addAttrisAndEles(Element element)
	{
		addAttributes(element);
		Element ele;
		ele = element.addElement(TagNames.INIT_STATE);
		initState.addAttrisAndEles(ele);
		
		ele = element.addElement(TagNames.INIT_TRANS);
		initTrans.addAttrisAndEles(ele);
		
		ele = element.addElement(TagNames.ROLES);
		Collection<Role> rols = roles.values();
		for(Role r : rols)
		{
			Element e = ele.addElement(TagNames.ROLE);
			r.addAttrisAndEles(e);
		}
		
	}
	
	public State getInitState()
	{
		return initState;
	}
	
	public Transition getInitTrans()
	{
		return initTrans;
	}
	
	public Map<String, Role> getRoles()
	{
		return roles;
	}
	
	public Task()
	{
		super();
		setDefaultVal();
	}
	public Task(String id, String name)
	{
		super(id, name);
		setDefaultVal();
	}
	
	public Task(Element element)
	{
		super();
		setDefaultVal();
		setIdAndName(element);
		Element ele;
		
		ele = element.element(TagNames.INIT_STATE);
		if(ele != null)
			this.initState = new State(ele);
		
		ele = element.element(TagNames.INIT_TRANS);
		this.initTrans = new Transition(ele);
		
		ele = element.element(TagNames.ROLES);
		if(ele != null)
		{
			List<Element> list = ele.elements(TagNames.ROLE);
			for(Element e : list)
			{
				Role role = new Role(e);
				if(!"".equals(role.getId()))
					roles.put(role.getId(), role);
			}
		}				
	}
	
	private void setDefaultVal()
	{
		initState = new State();
		initTrans = new Transition();
		roles = new TreeMap<String, Role>();
	}
	
	/*private List<String> list = new ArrayList<String>();
	public List<String> getList()
	{		
		return list;
	}*/
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub	
		Transition trans = new Transition("2", "yanht");
		System.out.println(trans.id);
		System.out.println(trans.name);
		System.out.println(trans.preStates.isEmpty());
		/*Task task = new Task();
		System.out.println(task.getList().size());
		List <String> li = task.getList();
		li.add("xxx");
		li.add("yyy");
		System.out.println(task.getList().size());*/
		
		
		
	}
}
